// pages/record_2/record_2.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    departmentArr: [],
    arr: [],
    didArr: [],
    none: [{ img: "https://lg-o8nytxik-1257013711.cos.ap-shanghai.myqcloud.com/微信图片_20180727121929.png", con: "暂无信息" }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id
    var that = this
    var val = JSON.parse(options.val)
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      backgroundColor: bgc,
      id: id,
      val: val
    })
    var con = options.con
    that.setData({
      con: con
    })
  },
  chooseProject: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var idx = e.currentTarget.dataset.idx
    var id = e.currentTarget.dataset.id
    var departmentArr = that.data.departmentArr
    var did = e.currentTarget.dataset.did
    var didArr = that.data.didArr
    var arr = that.data.arr
    console.log(departmentArr, did);
    departmentArr[index].projectArr[idx].checked = !departmentArr[index].projectArr[idx].checked
    console.log(departmentArr)
    that.setData({
      departmentArr: departmentArr,
    })
  },
  //深度复制数组
  deepcopy: function (obj) {
    var out = [],
      i = 0,
      len = obj.length;
    for (; i < len; i++) {
      if (obj[i] instanceof Array) {
        out[i] = deepcopy(obj[i]);
      } else out[i] = obj[i];
    }
    return out;
  },
  nextClick: function () {
    var that = this
    var arr = that.data.departmentArr
    var val = JSON.stringify(that.data.val)
    var arr1 = JSON.parse(JSON.stringify(arr))
    // var departmentArr1 = Array.from(arr1)
    console.log(arr)
    for (var i = 0; i < arr1.length; i++) {
      for (var j = 0; j < arr1[i].projectArr.length; j++) {
        if (!arr1[i].projectArr[j].checked) {
          arr1[i].projectArr.splice(j, 1);
          j--
        }
      }
      if (arr1[i].projectArr.length == 0) {
        arr1.splice(i, 1);
        i--
      }
    }
    console.log(arr1)
    var str = JSON.stringify(arr1)
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/record_3/record_3?con=' + that.data.con + '&str=' + str + '&val=' + val,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getSelpar()
    this.getAllparzilei()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //查询父类的所有子类
  getAllparzilei: function () {
    var that = this
    var id = that.data.id
    app.util.request({
      'url': 'entry/wxapp/Allparzilei',
      data: {
        id: id
      },
      success: function (res) {
        console.log(res)
        for (var i = 0; i < res.data.data.length; i++) {
          for (var j = 0; j < res.data.data[i].projectArr.length; j++) {
            res.data.data[i].projectArr[j].checked = false
          }
        }
        that.setData({
          departmentArr: res.data.data
        })
      }
    })
  },
  getSelpar: function () {
    var that = this
    var id = that.data.id
    app.util.request({
      'url': 'entry/wxapp/Selpar',
      data: {
        id: id
      },
      success: function (res) {
        console.log(res)
        that.setData({
          resdata: res.data.data
        })
      }
    })
  }
})